// ActionScript file
import Components.TitleWindow.twSaveCustomer;

import flash.events.KeyboardEvent;
import flash.ui.Keyboard;

import mx.controls.Alert;
import mx.events.CloseEvent;
import mx.events.FlexEvent;
import mx.managers.PopUpManager;

import org.flexlancer.Logger.Logger;
import org.flexlancer.databases.Customers;
import org.flexlancer.enums.EmbedResources;

[Bindable]
private var dpCustomerType:Array;

[Bindable]
private var customerData:Array = new Array();

private var completecustomerdata:Array = new Array();

private function init():void{
	try{
		dpCustomerType = EmbedResources.CUSTOMER_TYPES;
		this.completecustomerdata = Customers.List();
		this.cmbCustomerType.selectedItem = null;
		this.ShowFilterData();
	}catch(err:Error){
		Logger.LogE(err);
	}
}
private function PerformEditClient():void{
	try{
		if ( this.dgCustomers.selectedItem != null ){
			var twDialog:twSaveCustomer = PopUpManager.createPopUp(this,twSaveCustomer,true) as twSaveCustomer;
			twDialog.addEventListener(FlexEvent.REMOVE,OnRemoveSaveCustomer);
			twDialog.EditObject = this.dgCustomers.selectedItem;
			PopUpManager.centerPopUp(twDialog);
		}else{
			Alert.show("Please select customer for edit","No Customer Selected");
		}
	}catch(err:Error){
		Logger.LogE(err);
	}
}
private function RefreshData():void{
	try{
		this.completecustomerdata = Customers.List();
		this.dgCustomers.dataProvider = this.completecustomerdata;
	}catch(err:Error){
		Logger.LogE(err);
	}
}
private function OnRemoveSaveCustomer(evt:FlexEvent):void{
	try{
		this.RefreshData();
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function ShowFilterData():void{
	try{
		this.customerData = new Array();
		
		for each ( var singleData:Object in this.completecustomerdata ){
			this.customerData.push(singleData);
		}
		
		var strValue:String = "";
		var i:Number = 0;
		var obj:Object;
		
		if( this.txtCustomerID != null){
			strValue = this.txtCustomerID.text;
			for( i = 0 ; i < this.customerData.length ; i++){
				obj = this.customerData[i];
				
				if ( String(obj.customerid).indexOf(strValue) < 0 ){
					this.customerData.splice(i,1);
					i --;
				}
			}
		}
		if ( this.txtCustomerName.text != "" ){
			strValue = this.txtCustomerName.text.toLowerCase();
			for ( i = 0 ; i < this.customerData.length ; i++ ){
				obj = this.customerData[i];
				
				if ( String(obj.customername).toLowerCase().indexOf(strValue) < 0 ){
					this.customerData.splice(i,1);
					i --;
				}
			}
		}
		if ( this.cmbCustomerType.selectedItem != null ){
			strValue = this.cmbCustomerType.selectedItem as String;
			for ( i = 0 ; i < this.customerData.length ; i++ ){
				obj = this.customerData[i];
				
				if ( String(obj.customertype).indexOf(strValue) < 0 ){
					this.customerData.splice(i,1);
					i --;
				}
			}
		}
		if ( this.txtNumberOfVehicles.text != "" ){
			strValue = this.txtNumberOfVehicles.text;
			for ( i = 0 ; i < this.customerData.length ; i++ ){
				obj = this.customerData[i];
				
				if ( String(obj.numvehicles).indexOf(strValue) < 0 ){
					this.customerData.splice(i,1);
					i --;
				}
			}
		}
		
		if ( this.txtLoyaltyID.text != "" ){
			strValue = this.txtLoyaltyID.text;
			for ( i = 0 ; i < this.customerData.length ; i++ ){
				obj = this.customerData[i];
				
				if ( String(obj.customerloyalty).indexOf(strValue) < 0 ){
					this.customerData.splice(i,1);
					i --;
				}
			}
		}
		
	}catch(err:Error){
		Logger.LogE(err);
	}
}
private function OnDeleteConfirmation(evt:CloseEvent):void{
	try{
		if ( evt.detail == Alert.YES ){
			Customers.Delete("customerid = "+this.dgCustomers.selectedItem.customerid);
			this.RefreshData();
		}
	}catch(err:Error){
		Logger.LogE(err);
	}
}
private function OnKeyUp(event:KeyboardEvent):void{
	try{
		if ( event.keyCode == Keyboard.DELETE){
			this.PerformDeleteCustomer();			
		}
	}catch( err:Error){
		Logger.LogE(err);
	}
}
private function PerformDeleteCustomer():void{
	try{
		if ( this.dgCustomers.selectedItem != null ){
			Alert.show("Are you sure to delete \""+this.dgCustomers.selectedItem.customername+"\" customer ?",
			"Delete Confirmation",
			Alert.YES|Alert.NO,
			this,
			this.OnDeleteConfirmation);
		}else{
			Alert.show("Please select any service for delete","Information");
		}
	}catch( err:Error){
		Logger.LogE(err);
	}
}
private function OntxtCustomerName_Changed():void{
	try{
		this.ShowFilterData();
	}catch(err:Error){
		Logger.LogE(err);
	}
}
private function OnCustomerID_Changed():void{
	try{
		this.ShowFilterData();
	}catch(err:Error){
		Logger.LogE(err);
	}
}
private function OnNumVehcles_Changed():void{
	try{
		this.ShowFilterData();
	}catch(err:Error){
		Logger.LogE(err);
	}
}
private function OnResetFilter_Clicked():void
{
	try{
		this.txtCustomerID.text = "";
		this.txtCustomerName.text = "";
		this.txtNumberOfVehicles.text = "";
		this.cmbCustomerType.selectedIndex = -1;
		this.txtLoyaltyID.text = "";
		this.ShowFilterData();
	}catch(err:Error){
		Logger.LogE(err);
	}
}
private function OnCustomerTypeChanged():void{
	try{
		this.ShowFilterData();
	}catch(err:Error){
		Logger.LogE(err);
	}
}
private function OnAddNewCustomer():void{
	try{
		var twDialog:twSaveCustomer = PopUpManager.createPopUp(this,twSaveCustomer,true) as twSaveCustomer;
		twDialog.addEventListener(FlexEvent.REMOVE,OnRemoveSaveCustomer);
		PopUpManager.centerPopUp(twDialog);
	}catch(err:Error){
		Logger.LogE(err);
	}
}